//
//  MessageCenterViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/7/24.
//  Copyright © 2019 王洋. All rights reserved.
//
#import "OrderMessageCell.h"
#import "IncomeViewController.h"
#import "OrderDetailViewController.h"
#import "MessageCenterViewController.h"

@interface MessageCenterViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property (strong, nonatomic) NSMutableArray <OrderMessageModel *>* orderMsgArray;

@end

@implementation MessageCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self request_OrderMessageData];
}

- (void)request_OrderMessageData {
    [SVProgressHUD showWithStatus:@"正在请求消息列表"];
    [SVProgressHUD dismissWithDelay:1];
    
//    NSMutableDictionary *dic = @{}.mutableCopy;
//    [dic setValue:@"1" forKey:@"curPage"];
//    [dic setValue:@"20" forKey:@"pageSize"];
//
//    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_INDEX_MESSAGE];
//    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
//
//        if ([data isKindOfClass:[NSDictionary class]]) {
//
//            NSString * code = [data valueForKey:@"code"];
//            if ([code isEqualToString:@"200"]) {
//
//                /*
//                 content = "\U62cd\U4e2d\U82f9\U679c\U624b\U673a";
//                 createTime = "";
//                 id = "";
//                 isRead = 0;
//                 send = 0;
//                 status = 0;
//                 title = "\U8ba2\U5355\U521b\U5efa";
//                 type = 2;
//                 userId = 3581657286258501;
//                 },
//                 */
//
//                NSArray *resultArray = [data valueForKey:@"data"];
//                if ([resultArray isKindOfClass:[NSArray class]]) {
//
////                    for (int i = 0; i < resultArray.count; i++) {
////                        NSDictionary *orderMsgDic = resultArray[i];
////                        OrderMessageModel *model = [[OrderMessageModel alloc] init];
////                        model.itemOrderStatus = [orderMsgDic valueForKey:@"title"];
////                        model.itemOrderImageUrl = [orderMsgDic valueForKey:@"title"];
////                        model.itemOrderMainTitle = [orderMsgDic valueForKey:@"title"];
////                        model.itemOrderPrice = [orderMsgDic valueForKey:@"title"];
////                        model.itemOrderTime = [orderMsgDic valueForKey:@"title"];
////                        [self.orderMsgArray addObject:model];
////                    }
//                }
//
//                if (resultArray.count == 0) {
    
                    [[TTNoResultManager shareManager] showNoResultWithStateInView:self.tableview errorCode:kTTViewStateNoData btnTitle:@"刷新试试" didPressButton:^{
                        
                        [self request_OrderMessageData];
                    }];
//
//                }
//
//            } else {
//
//                //检查到未登录
//                if ([code isEqualToString:@"401"]) {
//                    [[TTUserModel sharedObject] initialization];
//
//                    [self openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
//                        if (isLogin) {
//                            [self request_OrderMessageData];
//                        }
//                    }];
//
//                } else {
//
//                    [[TTNoResultManager shareManager] showNoResultWithStateInView:self.tableview errorCode:kTTViewStateNoData btnTitle:@"刷新试试" didPressButton:^{
//                        [self request_OrderMessageData];
//                    }];
//
//                }
//                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"msg"]];
//                [SVProgressHUD dismissWithDelay:1];
//            }
//
//        } else {
//
//        }
//
//        [self.tableview reloadData];
//
//    } FailedBlock:^(NSError * _Nonnull error) {
//
//        WEAKSELF
//        [[TTNoResultManager shareManager] showNoResultWithStateInView:self.view errorCode:error.code btnTitle:@"重新加载" didPressButton:^{
//            [weakSelf request_OrderMessageData];
//        }];
//
//    }];
}

- (void)initView {
    self.title = @"消息中心";

    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableview registerNib:[UINib nibWithNibName:@"OrderMessageCell" bundle:nil] forCellReuseIdentifier:@"OrderMessageCellID"];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    OrderMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderMessageCellID" forIndexPath:indexPath];
    [cell fillCellWithModel:self.orderMsgArray[indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 140;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.orderMsgArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        OrderDetailViewController *VC = [[OrderDetailViewController alloc] init];
        VC.orderID = self.orderMsgArray[indexPath.row].itemOrderID;
        [self.navigationController pushViewController:VC animated:YES];
}

- (NSMutableArray<OrderMessageModel *> *)orderMsgArray {
    if (!_orderMsgArray) {
        _orderMsgArray = @[].mutableCopy;
    }
    return _orderMsgArray;
}

@end
